﻿using DevExpress.XtraEditors;
using GrupFirmalarTahsilatGirisi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TahsilatGirisi
{
    public partial class frmMain : XtraForm
    {
        private int sonSecim = 0;
        private DataTable tmpDt = new DataTable();
        public frmMain()
        {
            InitializeComponent();
            cmbhedeffirma.SelectedIndexChanged += cmbhedeffirma_SelectedIndexChanged;
            cmbhedefadi.ButtonClick += cmbhedefadi_ButtonClick;
            cmbhedefkod.ButtonClick += cmbhedefkod_ButtonClick;
            cmbkaynakadi.ButtonClick += cmbkaynakadi_ButtonClick;
            cmbkaynakfirma.SelectedIndexChanged += cmbkaynakfirma_SelectedIndexChanged;
            cmbkaynakKod.ButtonClick += cmbkaynakKod_ButtonClick;
            this.Load += frmMain_Load;
            this.KeyDown += frmMain_KeyDown;
            simpleButton1.Click += simpleButton1_Click;
            this.timer1.Tick += timer1_Tick;
            toolStripButton1.Click += toolStripButton1_Click;
            txtseri.Leave += txtseri_Leave;
            repositoryItemButtonEdit1.ButtonClick += repositoryItemButtonEdit1_ButtonClick;
            repositoryItemButtonEdit1.KeyDown += repositoryItemButtonEdit1_KeyDown;
            repositoryItemComboBox1.SelectedIndexChanged += repositoryItemComboBox1_SelectedIndexChanged;
            repositoryItemTextEdit1.KeyDown += repositoryItemTextEdit1_KeyDown;

        }

        void repositoryItemTextEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt == false && e.KeyCode == Keys.F4)
            {
                Kopyala();
            }
            else if (e.Alt == true && e.KeyCode == Keys.Q)
            {
                Sil();
            }
            else if (e.KeyCode == Keys.Insert)
            {
                Insert();
            }
        }

        void repositoryItemButtonEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt == false && e.KeyCode == Keys.F4)
            {
                Kopyala();
            }
            else if (e.Alt == true && e.KeyCode == Keys.Q)
            {
                Sil();
            }
            else if (e.KeyCode == Keys.F10)
            {
                SatirF10();
            }
            else if (e.KeyCode == Keys.Insert)
            {
                Insert();
            }
        }

        private void Insert()
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr != null && dr["No"] != DBNull.Value)
            {
                int curRow = Convert.ToInt32(dr["No"]);
                DataRow toInsert = tmpDt.NewRow();
                tmpDt.Rows.InsertAt(toInsert, curRow);
            }
        }

        void repositoryItemComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.sonSecim = ((ComboBoxEdit)sender).SelectedIndex;
            if (this.sonSecim < 5)
            {
                this.gridView1.Columns["GRUP"].OptionsColumn.AllowEdit = false;
            }
            if (this.sonSecim == 5)
            {
                this.gridView1.Columns["GRUP"].OptionsColumn.AllowEdit = true;
            }
            if (this.sonSecim == 6)
            {
                this.gridView1.Columns["GRUP"].OptionsColumn.AllowEdit = true;
            }

            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0], ((ComboBoxEdit)sender).SelectedItem);
        }

        void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            SatirF10();
        }

        private void SatirF10()
        {
            frmRehber rehber = new frmRehber
            {
                rehberList = this.sonSecim,
                firmano = (int)this.cmbhedeffirma.SelectedValue
            };
            rehber.ShowDialog();
            this.gridView1.EditingValue = rehber.seciliKod;
        }

        void txtseri_Leave(object sender, EventArgs e)
        {
            this.txtSira.Text = islemlerim.sonEvarkNo(this.txtseri.Text);
        }

        void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Evrak Silinsin mi  ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
            {
                foreach (int row in gridView2.GetSelectedRows())
                {
                    string seri = this.gridView2.GetRowCellValue(row, "SERI").ToString();
                    string sira = this.gridView2.GetRowCellValue(row, "SIRA").ToString();
                    islemlerim.DeleteEvrak(seri, sira);
                }
                this.gridControl2.DataSource = islemlerim.load();
            }
        }
        private void tmpDt_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            toplamHesapla();
        }

        private void toplamHesapla()
        {
            this.lblTutar.Text = Convert.ToDecimal(this.tmpDt.Compute("Sum(Tutar)", "") != DBNull.Value ? this.tmpDt.Compute("Sum(Tutar)", "") : 0).ToString("n2");
        }

        private void tmpDt_RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            toplamHesapla();
        }

        private void tmpDt_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            toplamHesapla();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.checkBox1.Checked)
            {
                this.gridColumn1.OptionsColumn.AllowEdit = false;
                this.gridColumn2.OptionsColumn.AllowEdit = false;
                this.gridColumn3.OptionsColumn.AllowEdit = false;
            }
            else
            {
                this.gridColumn1.OptionsColumn.AllowEdit = true;
                this.gridColumn2.OptionsColumn.AllowEdit = true;
                this.gridColumn3.OptionsColumn.AllowEdit = true;
            }
        }

        void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.cmbhedeffirma.Text != this.cmbkaynakfirma.Text)
            {
                foreach (DataRow row in this.tmpDt.Rows)
                {
                    islemlerim.kayitEt(this.txtseri.Text, Convert.ToInt32(this.txtSira.Text), (int)this.cmbkaynakfirma.SelectedValue, this.cmbkaynakCins.SelectedIndex, this.cmbkaynakKod.Text, this.cmbkaynaksorm.SelectedValue.ToString(), (int)this.cmbhedeffirma.SelectedValue, this.cmbhedefCins2.SelectedIndex, this.cmbhedefkod.Text, this.cmbhedefsorum.SelectedValue.ToString(), row["Cins"].ToString(), row["Hesap"].ToString(), row["Aciklama"].ToString(), Convert.ToSingle(row["Tutar"].ToString()), this.dtbelgetarihi.DateTime.Date, this.txtbelgeno.Text, row["GRUP"].ToString());
                }
                MessageBox.Show("Evrak Aktarımı Yapıldı", "İşlem", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                YeniEvrak();
            }
            else
            {
                MessageBox.Show("Firmalar Aynı Olamaz", "İşlem", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void YeniEvrak()
        {
            tmpDt.Rows.Clear();
            cmbkaynakKod.Text = "";
            cmbkaynakadi.Text = "";
            cmbkaynakCins.SelectedIndex = 0;
            cmbkaynakfirma.SelectedIndex = 0;
            cmbhedeffirma.SelectedIndex = 0;
            cmbhedefCins2.SelectedIndex = 0;
            cmbhedefkod.Text = "";
            cmbhedefadi.Text = "";

            if (cmbkaynaksorm.Properties.Items.Count > 0)
                cmbkaynaksorm.SelectedIndex = 0;

            if (cmbhedefsorum.Properties.Items.Count > 0)
                cmbhedefsorum.SelectedIndex = 0;

            dtTarih.DateTime = DateTime.Now.Date;
            dtbelgetarihi.DateTime = DateTime.Now.Date;
            txtbelgeno.Text = "";
            txtSira.Text = "";
            txtseri.Focus();
        }


        void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F12)
            {
                new frmAyar().ShowDialog();
            }
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            this.tmpDt.TableNewRow += new DataTableNewRowEventHandler(this.tmpDt_TableNewRow);
            this.tmpDt.RowChanged += new DataRowChangeEventHandler(this.tmpDt_RowChanged);
            this.tmpDt.RowDeleted += new DataRowChangeEventHandler(this.tmpDt_RowDeleted);
            try
            {
                this.gridControl2.DataSource = islemlerim.load();
            }
            catch (Exception)
            {
                new frmAyar().ShowDialog();
                Application.Exit();
            }
            this.cmbhedeffirma.DisplayMember = "fir_unvan";
            this.cmbhedeffirma.ValueMember = "fir_sirano";
            this.cmbhedeffirma.DataSource = islemlerim.firmaList();
            this.cmbhedeffirma.SelectedIndex = -1;
            this.cmbkaynakfirma.DisplayMember = "fir_unvan";
            this.cmbkaynakfirma.ValueMember = "fir_sirano";
            this.cmbkaynakfirma.DataSource = islemlerim.firmaList();
            this.cmbkaynakfirma.SelectedIndex = -1;
            this.tmpDt.Columns.Add("No", System.Type.GetType("System.Int32"));
            this.tmpDt.Columns.Add("Cins", System.Type.GetType("System.String"));
            this.tmpDt.Columns.Add("Hesap", System.Type.GetType("System.String"));
            this.tmpDt.Columns.Add("Projekod", System.Type.GetType("System.String"));
            this.tmpDt.Columns.Add("Aciklama", System.Type.GetType("System.String"));
            this.tmpDt.Columns.Add("Tutar", System.Type.GetType("System.Decimal"));
            this.tmpDt.Columns.Add("GRUP", System.Type.GetType("System.String"));
            this.gridControl1.DataSource = this.tmpDt;
            gridView1.BestFitColumns();
            this.txtseri.Text = db_man.Seri;
            this.txtseri.Focus();
        }

        void cmbkaynakKod_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmRehber rehber = new frmRehber
            {
                rehberList = this.cmbkaynakCins.SelectedIndex,
                firmano = (int)this.cmbkaynakfirma.SelectedValue
            };
            rehber.ShowDialog();
            this.cmbkaynakKod.Text = rehber.seciliKod;
            this.cmbkaynakadi.Text = rehber.seciliAd;
        }

        void cmbkaynakfirma_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.cmbkaynaksorm.DataSource = islemlerim.hesapGetir(10, this.cmbkaynakfirma.SelectedValue.ToString());
                this.cmbkaynaksorm.DisplayMember = "Ad";
                this.cmbkaynaksorm.ValueMember = "Kod";
            }
            catch (Exception)
            {
            }
        }

        void cmbkaynakadi_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmRehber rehber = new frmRehber
            {
                rehberList = this.cmbkaynakCins.SelectedIndex,
                firmano = (int)this.cmbkaynakfirma.SelectedValue
            };
            rehber.ShowDialog();
            this.cmbkaynakKod.Text = rehber.seciliKod;
            this.cmbkaynakadi.Text = rehber.seciliAd;
        }

        void cmbhedefkod_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmRehber rehber = new frmRehber
            {
                rehberList = this.cmbhedefCins2.SelectedIndex,
                firmano = (int)this.cmbhedeffirma.SelectedValue
            };
            rehber.ShowDialog();
            this.cmbhedefkod.Text = rehber.seciliKod;
            this.cmbhedefadi.Text = rehber.seciliAd;
        }

        void cmbhedefadi_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmRehber rehber = new frmRehber
            {
                rehberList = this.cmbhedefCins2.SelectedIndex,
                firmano = (int)this.cmbhedeffirma.SelectedValue
            };
            rehber.ShowDialog();
            this.cmbhedefkod.Text = rehber.seciliKod;
            this.cmbhedefadi.Text = rehber.seciliAd;
        }

        void cmbhedeffirma_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbkaynakKod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                cmbkaynakKod_ButtonClick(cmbkaynakKod, null);
            }
        }

        private void cmbkaynakadi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                cmbkaynakadi_ButtonClick(cmbkaynakadi, null);
            }
        }

        private void cmbhedefkod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                cmbhedefkod_ButtonClick(cmbhedefkod, null);
            }
        }

        private void cmbhedefadi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F10)
            {
                cmbhedefadi_ButtonClick(cmbhedefadi, null);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            gridView1.Focus();
        }

        private void Kopyala()
        {
            int curRow = 0;
            int prevRow = 0;
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr != null && dr["No"] != DBNull.Value)
            {
                curRow = Convert.ToInt32(dr["No"]);
            }
            else
            {
                curRow = gridView1.RowCount - 1;
                gridView1.AddNewRow();
                gridView1.FocusedRowHandle = curRow;
                gridView1.RefreshRow(curRow);
                gridView1.FocusedRowHandle = curRow;
            }

            if (curRow > 0)
                prevRow = curRow - 1;

            foreach (DevExpress.XtraGrid.Columns.GridColumn item in gridView1.Columns)
            {
                if (item.Name != "No")
                {
                    gridView1.SetRowCellValue(curRow, item, gridView1.GetRowCellValue(prevRow, item));
                }
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt == false && e.KeyCode == Keys.F4)
            {
                Kopyala();
            }
            else if (e.Alt == true && e.KeyCode == Keys.Q)
            {
                Sil();
            }
            else if (e.KeyCode == Keys.Insert)
            {
                Insert();
            }
        }

        private void Sil()
        {
            DataRow dr = gridView1.GetFocusedDataRow();
            if (dr != null && dr["No"] != DBNull.Value)
            {
                int curRow = Convert.ToInt32(dr["No"]);
                gridView1.DeleteRow(curRow);
            }
        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                gridView1.SetRowCellValue(i, gridView1.Columns["No"], i);
            }
        }

        private void gridView1_RowCountChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                gridView1.SetRowCellValue(i, gridView1.Columns["No"], i);
            }
        }
    }
}
